﻿// <copyright file="SourceItemComparer.cs" company="Soumya Chattopadhyay">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <disclaimer> This software is intented for educational purposes only and not for 
// commerical or prodution use. The software is provided "as is", without warranty 
// of any kind, express or implied, including but not limited to the warranties of 
// merchantability, fitness for a particular purpose and noninfringement. In no event 
// shall the author(s) or copyright holder(s) be liable for any claim, damages or other 
// liability, whether in an action of contract, tort or otherwise, arising from, out of 
// or in connection with the software or the use or other dealings in the software.
// </disclaimer>

namespace My.CodePlex.TFSExplorer
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using My.CodePlex.TFSExplorer.TeamFoundation;

    /// <summary>
    /// Compares soureitems
    /// </summary>
    public class SourceItemComparer : ListViewCustomComparer<SourceItem>
    {
        /// <summary>
        /// Compares the specified x to y.
        /// </summary>
        /// <param name="x">The x sourceitem</param>
        /// <param name="y">The y sourceitem</param>
        /// <returns>The comparison result</returns>
        public override int Compare(SourceItem x, SourceItem y)
        {
            try
            {
                string valueX = string.Empty, valueY = string.Empty;
                switch (SortBy)
                {
                    default:
                    case "Name":
                        valueX = x.Name;
                        valueY = y.Name;
                        break;
                    case "CheckinDate":
                        {
                            DateTime t1 = DateTime.Parse((string)x.CheckinDate);
                            DateTime t2 = DateTime.Parse((string)y.CheckinDate);
                            if (SortDirection.Equals(ListSortDirection.Ascending))
                            {
                                return DateTime.Compare(t1, t2);
                            }
                            else
                            {
                                return DateTime.Compare(t2, t1);
                            }
                        }
                }

                if (SortDirection.Equals(ListSortDirection.Ascending))
                {
                    return String.Compare(valueX, valueY);
                }
                else
                {
                    return (-1) * String.Compare(valueX, valueY);
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}
